<!DOCTYPE html>
<html lang="en">

<head>
    <title>非父子组件的通讯</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- <link href="css/style.css" rel="stylesheet"> -->
</head>

<body>
    <div id="app">
        <my-header></my-header>
        <my-list></my-list>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>

    <script>
        var busVm = new Vue(); //空实例
        var vm = new Vue({
            el: "#app",
            components: {
                "my-header": {
                    template: `<h2>{{message}}</h2>`,
                    data: function () {
                        return {
                            message: "hello myHeader"
                        }
                    },
                    mounted: function () {
                        busVm.$on('changeEvents', function (str) {
                            this.message = str;
                            console.log(str);
                        }.bind(this));
                    }
                },
                "my-list": {
                    template: '<ul><li @click="getContent" v-for="item in list">{{item}}</li></ul>',
                    data: function () {
                        return {
                            list: ['first', 'second', 'three']
                        }
                    },
                    methods: {
                        getContent: function (ev) {
                            busVm.$emit('changeEvents', ev.target.innerHTML)
                        }
                    }
                }
            }
        });
    </script>
</body>

</html>